
#include "Paineis/PainelA.h"
#include "PetLab.h"


PainelA::PainelA(int cx, int cy, int x, int y)
    : Controle(cx, cy, x, y)
{
    for(int i=0; i<Largura(); i++)
        for(int j=0; j<Altura(); j++)
        {
            int raio = Largura();
            int x = i - Largura()/2;
            int y = j - Altura()/2;
			double fator = ((double)::sqrt((float)x*x+y*y))/(raio);
            
            int r = (int)(fator * 64/2)+128+32;
            int g = (int)(fator * 64/2)+128+64+32;
            int b = 255;
            Matriz()[i][j] = RGBA(r,g,b,64);
        }
    
    for(int i=0; i<Largura(); i++)
    {
        Matriz()[i][0]          = RGBA(128,255,128+64+32,128+64);
        Matriz()[i][Altura()-1] = RGBA(128,255,128+64+32,128+64);
    }
    for(int j=0; j<Altura(); j++)
    {
        Matriz()[0][j]           = RGBA(128,255,128+64+32,128+64);
        Matriz()[Largura()-1][j] = RGBA(128,255,128+64+32,128+64);
    }

    Matriz()[0][0] &= 0x00FFFFFF;
    Matriz()[0][Altura()-1] &= 0x00FFFFFF;
    Matriz()[Largura()-1][0] &= 0x00FFFFFF;
    Matriz()[Largura()-1][Altura()-1] &= 0x00FFFFFF;
    
    
    
    // Adiciona TELA_A
    
    m_TelaA = new TelaA(5, 27);
    m_TelaA->Visivel(false);
    //m_TelaA->Abrir();
    m_TelaA->CarregarFuncao(NULL);
    this->AdicionarControle(m_TelaA);

    m_BotaoRe = new BotaoRe(0,0,&TelaA::ParteRe,m_TelaA);
    this->AdicionarControle(m_BotaoRe);

    m_BotaoIm = new BotaoIm(0,0,&TelaA::ParteIm,m_TelaA);
    this->AdicionarControle(m_BotaoIm);



    m_checkR = new BotaoCheckCor(404+50,361+50,RGBA(255,192,192,128),RGBA(255-64,0,0,128),RGBA(255-128-64+32,0,0,128), &TelaA::BandaR);
    m_checkR->Visivel(false);
    this->AdicionarControle(m_checkR);
    m_checkG = new BotaoCheckCor(404+67,361+50,RGBA(192,255,192,128),RGBA(0,255-64,0,128),RGBA(0,255-128-64+32,0,128), &TelaA::BandaG);
    m_checkG->Visivel(false);
    this->AdicionarControle(m_checkG);
    m_checkB = new BotaoCheckCor(404+84,361+50,RGBA(192,192,255,128),RGBA(0,0,255-64,128),RGBA(0,0,255-128-64+32,128), &TelaA::BandaB);
    m_checkB->Visivel(false);
    this->AdicionarControle(m_checkB);

    m_FecharPainel = new BotaoFecharPainel(20, 19, Largura()-25, 4);
    m_FecharPainel->Visivel(false);
    this->AdicionarControle(m_FecharPainel);

}

void PainelA::AbrirImagem(std::string strNomeDoArquivo)
{
    PainelC&  painelC  = *(g_PetLab->m_PainelC);
    PainelD&  painelD  = *(g_PetLab->m_PainelD);
    painelC.Visivel(false);
    painelD.Visivel(false);

    TelaA&    telaA    = *(g_PetLab->m_PainelA->Tela());
    TelaB&    telaB    = *(g_PetLab->m_PainelB->Tela());
    PainelA&  painelA  = *(g_PetLab->m_PainelA);
    PainelB&  painelB  = *(g_PetLab->m_PainelB);
    ComboBox& dominio  = *(g_PetLab->m_PainelB->Dominio());
    int       cxPetLab = g_PetLab->Largura();
    int       cyPetLab = g_PetLab->Altura();


    if( telaA.Abrir(strNomeDoArquivo) == false )
        return ;

    painelA.Visivel(false);
    painelB.Visivel(false);
    telaA.Visivel(false);
    telaB.Visivel(false);
    dominio.Visivel(false);
    g_PetLab->m_PainelA->CheckR()->Visivel(false);
    g_PetLab->m_PainelA->CheckG()->Visivel(false);
    g_PetLab->m_PainelA->CheckB()->Visivel(false);
    
    if(g_PetLab->m_PainelA->CheckR()->Checked())
        telaA.BandaR(true);
    else
        telaA.BandaR(false);
        
    if(g_PetLab->m_PainelA->CheckG()->Checked())
        telaA.BandaG(true);
    else
        telaA.BandaG(false);
        
    if(g_PetLab->m_PainelA->CheckB()->Checked())
        telaA.BandaB(true);
    else
        telaA.BandaB(false);
        
    if(telaA.Largura() > 450)
        telaA.Largura(450);
        
    if(telaA.Altura() > 400)
        telaA.Altura(400);
    
    telaB.Carregar(&telaA);

    /*int xTelaA = 6;
    int xTelaB = 6;
    
    telaA.PosicaoAbsoluta(xTelaA, telaA.PosicaoY());
    telaB.PosicaoAbsoluta(xTelaB, telaB.PosicaoY());*/
    
    int xPainelA = cxPetLab/2 - telaA.Largura() - 15;

    painelA.PosicaoAbsoluta(xPainelA, painelA.PosicaoY());
    
    int xPainelB = cxPetLab/2+5;
    int yPainelB = painelB.PosicaoY();
    
    painelB.PosicaoAbsoluta(xPainelB, yPainelB);

    int cxPainelA = telaA.Largura()+10;
    int cyPainelA = telaA.Altura()+32+20;
    int cxPainelB = telaB.Largura()+10;
    int cyPainelB = telaB.Altura()+32+20;
    
    if(cxPainelA < 90)
        cxPainelA = 90;
    if(cxPainelB < 90)
        cxPainelB = 90;
    
    if(cyPainelA < 100)
        cyPainelA = 100;
    if(cyPainelB < 100)
        cyPainelB = 100;
    
    painelA.Redimensionar(cxPainelA, cyPainelA);
    painelB.Redimensionar(cxPainelB, cyPainelB);

    painelA.Visivel(true);
    painelB.Visivel(true);
    telaA.Visivel(true);
    telaB.Visivel(true);

    int xDominioA = painelB.Largura() - g_PetLab->m_PainelB->Dominio()->Largura() - 5;
    int yDominioA = g_PetLab->m_PainelB->Dominio()->PosicaoY();
    g_PetLab->m_PainelB->Dominio()->PosicaoAbsoluta(xDominioA, yDominioA);

    g_PetLab->m_PainelB->Dominio()->Visivel(true);
    
    int xCheckR = /*painelA.PosicaoX()+*/painelA.Largura()-50+2 -5;
    int yCheckR = /*painelA.PosicaoY()+*/painelA.Altura()-10+15 -25;
    g_PetLab->m_PainelA->CheckR()->PosicaoAbsoluta(xCheckR, yCheckR);
    g_PetLab->m_PainelA->CheckR()->Visivel(true);

    int xCheckG = /*painelA.PosicaoX()+*/painelA.Largura()-50+2+17 -5;
    int yCheckG = /*painelA.PosicaoY()+*/painelA.Altura()-10+15 -25;
    g_PetLab->m_PainelA->CheckG()->PosicaoAbsoluta(xCheckG, yCheckG);
    g_PetLab->m_PainelA->CheckG()->Visivel(true);

    int xCheckB = /*painelA.PosicaoX()+*/painelA.Largura()-50+2+2*17 -5;
    int yCheckB = /*painelA.PosicaoY()+*/painelA.Altura()-10+15 -25;
    g_PetLab->m_PainelA->CheckB()->PosicaoAbsoluta(xCheckB, yCheckB);
    g_PetLab->m_PainelA->CheckB()->Visivel(true);
    
    int xBotaoRe = 5;
    int yBotaoRe = painelA.Altura() - 21;
    painelA.botaoRe()->PosicaoX(xBotaoRe);
    painelA.botaoRe()->PosicaoY(yBotaoRe);
    painelB.botaoRe()->PosicaoX(xBotaoRe);
    painelB.botaoRe()->PosicaoY(yBotaoRe);

    int xBotaoIm = 23;
    int yBotaoIm = painelA.Altura() - 21;
    painelA.botaoIm()->PosicaoX(xBotaoIm);
    painelA.botaoIm()->PosicaoY(yBotaoIm);
    painelB.botaoIm()->PosicaoX(xBotaoIm);
    painelB.botaoIm()->PosicaoY(yBotaoIm);
    
    g_PetLab->m_PainelA->botaoFecharPainel()->PosicaoX(g_PetLab->m_PainelA->Largura()-25);
    g_PetLab->m_PainelA->botaoFecharPainel()->Visivel(true);
}

void PainelA::Redimensionar(int cx, int cy)
{
    Largura(cx);
    Altura(cy);
    
    for(int i=0; i<Largura(); i++)
        for(int j=0; j<Altura(); j++)
        {
            int raio = Largura();
            int x = i - Largura()/2;
            int y = j - Altura()/2;
			double fator = ((double)::sqrt((float)x*x+y*y))/(raio);
            
            int r = (int)(fator * 64/2)+128+32;
            int g = (int)(fator * 64/2)+128+64+32;
            int b = 255;
            Matriz()[i][j] = RGBA(r,g,b,64);
        }
    
    for(int i=0; i<Largura(); i++)
    {
        Matriz()[i][0]          = RGBA(128,255,128+64+32,128+64);
        Matriz()[i][Altura()-1] = RGBA(128,255,128+64+32,128+64);
    }
    for(int j=0; j<Altura(); j++)
    {
        Matriz()[0][j]           = RGBA(128,255,128+64+32,128+64);
        Matriz()[Largura()-1][j] = RGBA(128,255,128+64+32,128+64);
    }

    Matriz()[0][0] &= 0x00FFFFFF;
    Matriz()[0][Altura()-1] &= 0x00FFFFFF;
    Matriz()[Largura()-1][0] &= 0x00FFFFFF;
    Matriz()[Largura()-1][Altura()-1] &= 0x00FFFFFF;
}


bool PainelA::AoBaixarEsq(EventoBaixarEsq ev)
{
    return false;
    
    
    for(int i=0; i<Largura(); i++)
        for(int j=0; j<Altura(); j++)
        {
            int raio = Largura();
            int x = i - Largura()/2;
            int y = j - Altura()/2;
			double fator = ((double)::sqrt((float)x*x+y*y))/(raio);
            
            int r = (int)(fator * 64/2)+128+64+32;
            int g = (int)(fator * 64/2)+128+32;
            int b = 32;
            Matriz()[i][j] = RGBA(r,g,b,128);
        }
    
    for(int i=0; i<Largura(); i++)
    {
        Matriz()[i][0]          = RGBA(255,255,0/*128+64+32*/,255);
        Matriz()[i][Altura()-1] = RGBA(255,255,0/*128+64+32*/,255);
    }
    for(int j=0; j<Altura(); j++)
    {
        Matriz()[0][j]           = RGBA(255,255,0/*128+64+32*/,255);
        Matriz()[Largura()-1][j] = RGBA(255,255,0/*128+64+32*/,255);
    }

    Matriz()[0][0] &= 0x00FFFFFF;
    Matriz()[0][Altura()-1] &= 0x00FFFFFF;
    Matriz()[Largura()-1][0] &= 0x00FFFFFF;
    Matriz()[Largura()-1][Altura()-1] &= 0x00FFFFFF;
    
    ev.janela->Redesenhar(this);
}


bool PainelA::AoSubirEsq(EventoSubirEsq ev)
{
    return false;


    for(int i=0; i<Largura(); i++)
        for(int j=0; j<Altura(); j++)
        {
            int raio = Largura();
            int x = i - Largura()/2;
            int y = j - Altura()/2;
			double fator = ((double)::sqrt((float)x*x+y*y))/(raio);
            
            int r = (int)(fator * 64/2)+128+32;
            int g = (int)(fator * 64/2)+128+64+32;
            int b = 255;
            Matriz()[i][j] = RGBA(r,g,b,64);
        }
    
    for(int i=0; i<Largura(); i++)
    {
        Matriz()[i][0]          = RGBA(128,255,128+64+32,128+64);
        Matriz()[i][Altura()-1] = RGBA(128,255,128+64+32,128+64);
    }
    for(int j=0; j<Altura(); j++)
    {
        Matriz()[0][j]           = RGBA(128,255,128+64+32,128+64);
        Matriz()[Largura()-1][j] = RGBA(128,255,128+64+32,128+64);
    }

    Matriz()[0][0] &= 0x00FFFFFF;
    Matriz()[0][Altura()-1] &= 0x00FFFFFF;
    Matriz()[Largura()-1][0] &= 0x00FFFFFF;
    Matriz()[Largura()-1][Altura()-1] &= 0x00FFFFFF;
    
    ev.janela->Redesenhar(this);
}
